package com.starnet.studentmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starnet.studentmanagement.entity.CourseSelection;
import com.starnet.studentmanagement.entity.Courses;
import com.starnet.studentmanagement.handler.ApiException;
import com.starnet.studentmanagement.mapper.CourseSelectionMapper;
import com.starnet.studentmanagement.mapper.CoursesMapper;
import com.starnet.studentmanagement.service.CoursesService;
import com.starnet.studentmanagement.util.PageUtil;
import com.starnet.studentmanagement.vo.PageResult;
import com.starnet.studentmanagement.vo.PageVO;
import com.starnet.studentmanagement.vo.Result;
import com.starnet.studentmanagement.vo.ResultCode;
import com.starnet.studentmanagement.vo.param.PageParams;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sangeng
 * @since 2022-03-26
 */
@Service
public class CoursesServiceImpl implements CoursesService {
    @Autowired
    CoursesMapper coursesMapper;
    @Autowired
    CourseSelectionMapper courseSelectionMapper;
    @Override
    public PageResult listPage(String cname, Integer page, Integer pageSize) {
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        IPage<Courses> iPage = new Page<>();
        if (cname!=null){
            queryWrapper.like(Courses::getCname,cname);
        }
        if (pageSize!=null){
            iPage.setSize(pageSize);
        }
        //设置页码等参数
        iPage.setCurrent(page);
        coursesMapper.selectPage(iPage, queryWrapper);
        if (iPage.getTotal()==0){
            throw new ApiException(ResultCode.EMPTY_RESULT.getCode(), "无匹配课程,查询结果为空");
        }
        PageVO pageVO = PageUtil.getPageVO(iPage);
        return PageResult.success(iPage.getRecords(), pageVO);
    }



    @Override
    public Result addNewCourse(Courses course) {
        if (course.getPassMark()<0||course.getPassMark()>100){
            return new Result(ResultCode.ILLEGAL_PARAM.getCode(),"课程及格分不符合规定",null);
        }
        //查询是否已经存在该课程
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getCname, course.getCname());
        Courses coursesByName = coursesMapper.selectOne(queryWrapper);
        if (coursesByName != null) {
            throw new ApiException(ResultCode.EXISTED.getCode(),"此课程名已存在，请更换");
        }
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getCid, course.getCid());
        Courses coursesByCid = coursesMapper.selectOne(queryWrapper);
        if (coursesByCid != null) {
            throw new ApiException(ResultCode.EXISTED.getCode(),"此课程号已存在，请更换");
        }
        //确认不存在后 新增
        int insert = coursesMapper.insert(course);
        if (insert == 0) {
            throw new ApiException(ResultCode.UNKNOWN_ERROR.getCode(), "新增失败，服务器未知错误");
        }
        return new Result(ResultCode.SUCCESS.getCode(),"新增成功",null);
    }

    @Override
    public Result updateCourse(Courses course) {
        //检查新课程号有没有被占用
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getCid,course.getCid());
        queryWrapper.ne(Courses::getId,course.getId());
        List<Courses> checkCid = coursesMapper.selectList(queryWrapper);
        System.out.println(checkCid.toString());
        System.out.println(checkCid.size());
        if (checkCid.size()!=0){
            throw new ApiException(ResultCode.EXISTED.getCode(), "更新失败，该课程号已存在");
        }
        //检查新课程名有没有被占用
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getCname,course.getCname());
        queryWrapper.ne(Courses::getId,course.getId());
        List<Courses> checkCname = coursesMapper.selectList(queryWrapper);
        if (checkCname.size()!=0){
            throw new ApiException(ResultCode.EXISTED.getCode(), "更新失败，该课程名已存在");
        }

        int update = coursesMapper.updateById(course);
        if (update == 0) {
           throw new ApiException(ResultCode.NOT_FOUND.getCode(), "更新失败，该条数据已被删除");
        }
        return new Result(ResultCode.SUCCESS.getCode(), "更新成功", null);
    }
    @Transactional
    @Override
    public Result deleteById(int id) {
        Courses courses = coursesMapper.selectById(id);
        if (courses == null) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(),"此课程不存在，删除失败");
        }
         coursesMapper.deleteById(id);
        LambdaQueryWrapper<CourseSelection> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSelection::getCid,courses.getCid());
        courseSelectionMapper.delete(queryWrapper);
        return new Result(ResultCode.SUCCESS.getCode(), "删除成功", null);
    }
}
